@extends('layouts.application')

@section('css')
    <link rel="stylesheet" href="/vendor/daterangepicker/daterangepicker.css">
@endsection

@section('content')
    <div class="admin-content">
        <div class="admin-content-body">
            <div class="am-cf am-padding am-padding-bottom-0">
                <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">商品回收列表</strong> /
                    <small>Product Recycle List</small>
                </div>
            </div>

            <br>

            <div class="am-g">
                <div class="am-u-sm-12">
                    <div class="am-btn-toolbar">
                        <div class="am-btn-group am-btn-group-xs">
                            <button type="button" class="reply_all am-btn am-btn-default"><span
                                        class="am-icon-reply"></span> 还原
                            </button>
                            <button type="button" class="destroy_all am-btn am-btn-danger"><span
                                        class="am-icon-trash-o"></span> 彻底删除
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <hr>



            <div class="am-g">
                <div class="am-u-sm-12">
                    <form class="am-form">
                        <table class="am-table am-table-striped am-table-hover table-main">
                            <thead>
                            <tr>
                                <th class="table-check">
                                    <input type="checkbox" class="check_all"/>
                                </th>
                                <th>ID</th>
                                <th>缩略图</th>
                                <th>商品名称</th>
                                <th>所属分类</th>
                                <th>品牌</th>
                                <th>单价</th>
                                <th>上架</th>
                                <th>置顶</th>
                                <th>推荐</th>
                                <th>热销</th>
                                <th>新品</th>
                                <th>库存</th>
                                <th>上架日期</th>
                                <th>操作</th>
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($products as $product)
                                <tr data-id="{{$product->id}}">
                                    <td>
                                        <input type="checkbox" name="check_id[]" value="{{$product->id}}"
                                               class="checked_id"/>
                                    </td>
                                    <td>{{$product->id}}</td>
                                    <td>
                                        <img src="{{$product->thumb}}" alt="" style="width: 50px;height: 50px;">
                                    </td>
                                    <td>{{$product->name}}</td>
                                    <td>{{$product->categories->implode('name',', ')}}</td>
                                    <td>{{$product->brand->name or ''}}</td>
                                    <td>{{$product->price}} 元</td>

                                    @foreach($datas as $data)
                                        <td>
                                            {!! is_something($product,$data) !!}
                                        </td>
                                    @endforeach

                                    <td>
                                        <input type="text" value="{{$product->stock}}" style="width: 50px;">
                                    </td>
                                    <td>{{$product->created_at}}</td>
                                    <td>
                                        <div class="am-btn-toolbar">
                                            <div class="am-btn-group am-btn-group-xs">
                                                <a href="javascript:;"
                                                   class="reply_one am-btn am-btn-default am-btn-xs am-text-secondary">
                                                    <span class="am-icon-pencil-reply"></span> 还原
                                                </a>
                                                <button class="delete am-btn am-btn-default am-btn-xs am-text-danger am-hide-sm-only">
                                                    <span class="am-icon-trash-o"></span> 删除
                                                </button>
                                            </div>
                                        </div>
                                    </td>
                                </tr>
                            @endforeach

                            </tbody>
                        </table>
                        <div class="am-cf">
                            共 {{$products->total()}} 条记录
                            <div class="am-fr">
                                <ul class="am-pagination">
                                    {{$products->links()}}
                                </ul>
                            </div>
                        </div>
                        <hr/>
                    </form>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <p class="am-padding-left">© 2014 AllMobilize, Inc. Licensed under MIT license.</p>
        </footer>
    </div>
@endsection

@section('js')

    <script>
        $(function () {
            //单个删除
            $('.delete').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);

                $.ajax({
                    type: 'DELETE',
                    url: '/admin/shop/product/' + id,
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                });
                return false;
            });

            //上面全选，下面也选中
            $('.check_all').click(function () {
                var checked = $(this).prop('checked');
                $('.checked_id').prop('checked', checked);
            });

            //多选删除
            $('.destroy_all').click(function () {
                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('请你至少选择一条');
                    return false;
                }

                $.ajax({
                    type: 'DELETE',
                    url: '{{route('shop.product.destroy_all')}}',
                    data: $('.checked_id:checked').serialize(),
                    success: function () {
                        $('.checked_id:checked').parents('tr').fadeOut(400);
                    }
                });
                return false;
            });

            //单个还原
            $('.reply_one').click(function () {
                var id = $(this).parents('tr').data('id');
                var _this = $(this);
                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.reply_one')}}',
                    data: {id: id},
                    success: function () {
                        _this.parents('tr').fadeOut(400);
                    }
                })
                return false;
            })

            //多个还原
            $('.reply_all').click(function () {
                var length = $('.checked_id:checked').length;
                if (length == 0) {
                    alert('请你至少选择一条');
                    return false;
                }

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.reply_all')}}',
                    data: $('.checked_id:checked').serialize(),
                    success: function () {
                        $('.checked_id:checked').each(function () {
                            $(this).parents('tr').fadeOut(400);
                        })
                    }
                });
                return false;
            });


            //改变属性
            $('.change_attr').click(function () {
                var id = $(this).parents('tr').data('id');
                var attr = $(this).attr('data-attr');
                var _this = $(this);

                $.ajax({
                    type: 'PATCH',
                    url: '{{route('shop.product.change_attr')}}',
                    data: {id: id, attr: attr},
                    success: function () {
                        _this.toggleClass('am-icon-check am-icon-close');
                    }
                });
                return false;
            });

        })
    </script>
@endsection